package com.TropicalFlavor.service;

import com.TropicalFlavor.dao.*;
import com.TropicalFlavor.po.MarketGoods;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Transactional
@Service
public class GoodsServiceImpl implements GoodsService{

    @Resource
    private GoodsDao goodsDao;

    @Resource
    private SalesDao salesDao;

    @Resource
    private UserDao userDao;

    @Override
    public boolean ChangeGoodsInfo(MarketGoods marketGoods)
    {
        return goodsDao.ChangeInfo(marketGoods);
    }

    @Override
    public List<MarketGoods> SearchGoods(String Keywords) {
        List<String> KeywordsList = new ArrayList<String>(Arrays.asList(Keywords.split("\\s+")));
        Set<MarketGoods> TempListWithMarketGoods = new HashSet<>();
        for (String s : KeywordsList) {
            TempListWithMarketGoods.addAll(goodsDao.SelectGoodsByName(s));
        }
        for (String s : KeywordsList) {
            TempListWithMarketGoods.addAll(goodsDao.SelectGoodsByKind(s));
        }
        return new ArrayList<>(TempListWithMarketGoods);

    }

    @Override
    public Integer getGoodsSum()
    {
        return goodsDao.getGoodsSum();
    }

    @Override
    public String WhoseGoods(String GID)
    {
        return salesDao.WhoseGoods(GID);
    }


    @Override
    public List<MarketGoods> selectTenGoods(Map map)
    {
        return goodsDao.selectTenGoods(map);
    }

    @Override
    public MarketGoods SelectGoodsByPK(String GID)
    {
        return goodsDao.SelectGoods(GID);
    }

    @Override
    public List<MarketGoods> SortByPrice(List<MarketGoods> goods, Boolean isASC) {
        goods.sort(new Comparator<MarketGoods>() {
            @Override
            public int compare(MarketGoods o1, MarketGoods o2) {
                return (int) (o1.getPrice() - o2.getPrice());
            }
        });
        return goods;
    }

    @Override
    public List<MarketGoods> SortByName(List<MarketGoods> goods, Boolean isASC) {
        goods.sort(new Comparator<MarketGoods>() {
            @Override
            public int compare(MarketGoods o1, MarketGoods o2) {
                return o2.getName().compareTo(o1.getName());
            }
        });
        return goods;
    }
}
